/**
 * 2017年11月16日
 */
package exp.algorithm.sic.scalerf.fea;

/**
 * @author Alex
 *
 */
public class TSFFeatureSet extends AbstractFeatrueSet{
	double mean;
	double stDev;
	double slope;
	@Override
	public void setFeatures(double[] data, int start, int end) {
		double sumX = 0, sumYY = 0;
		double sumY = 0, sumXY = 0, sumXX = 0;
		int length = end - start + 1;
		for (int i = start; i <= end; i++) {
			sumY += data[i];
			sumYY += data[i] * data[i];
			sumX += (i - start);
			sumXX += (i - start) * (i - start);
			sumXY += data[i] * (i - start);
		}
		mean = sumY / length;
		stDev = sumYY - (sumY * sumY) / length;
		slope = (sumXY - (sumX * sumY) / length);
		if (sumXX - (sumX * sumX) / length != 0)
			slope /= sumXX - (sumX * sumX) / length;
		else
			slope = 0;
		stDev /= length;
		if (stDev == 0) // Flat line
			slope = 0;
		// else
		// stDev=Math.sqrt(stDev);
		if (slope == 0)
			stDev = 0;
	}
	@Override
	public double get(int idx) {
		if(idx == 0) return mean;
		if(idx == 1) return stDev;
		else return slope;
	}

}
